{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Decord AudioReader Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys, os\n",
    "sys.path.insert(0, os.path.abspath('../python'))\n",
    "import decord as de\n",
    "import simpleaudio as sa\n",
    "import numpy as np\n",
    "from scipy.io.wavfile import write"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Take a simple audio file as exmaple here\n",
    "To be noticed, AudioReader could read video files too as long as there's audio track."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "ctx = de.cpu(0)\n",
    "audio_file = 'example.mp3'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### You can choose whether to use the default sample rate or default channel layout.\n",
    "Specifying mono=true would decode you a mono audio track"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shape of audio samples:  (2, 7708092)\n"
     ]
    }
   ],
   "source": [
    "ar = de.AudioReader(audio_file, ctx, mono=False)\n",
    "print('Shape of audio samples: ', ar.shape() )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Similar to VideoReader, decoding file-like object in memory works for AudioReader too"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shape of audio samples:  (1, 7708092)\n"
     ]
    }
   ],
   "source": [
    "with open(audio_file, 'rb') as f:\n",
    "    ar = de.AudioReader(f, ctx)\n",
    "    print('Shape of audio samples: ', ar.shape() )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Acessing audio samples is just easy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The first sample:  [-0.00052051]\n",
      "The first five samples:  [[-0.00052051 -0.00064157 -0.00056435 -0.00055603 -0.00059031]]\n"
     ]
    }
   ],
   "source": [
    "print('The first sample: ', ar[0])\n",
    "print('The first five samples: ', ar[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# simple function to play the audio\n",
    "def play(audio, num_channel, sample_rate, file_to_write):\n",
    "    buffer = audio.asnumpy()\n",
    "    player = sa.play_buffer(buffer, num_channel, 4, sample_rate)\n",
    "    player.wait_done()\n",
    "    write(file_to_write, sample_rate, buffer.T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's play some music\n",
    "Here is the link to the audio: https://sndup.net/5vx2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_rate = 44100\n",
    "end = sample_rate * 5\n",
    "play(ar[0:end], 1, sample_rate, \"demo_44100.wav\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### It could be useful to lower the sample rate if you don't care the sound quality ;)\n",
    "One caveat here is playing multi channel raw audio can be tricky because of the channel layout.\n",
    "Decord simply provide you the raw data. You are on your own if you want to play it.\n",
    "Here is the link to the audio: https://sndup.net/3kn2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shape of audio samples:  (1, 7708092)\n"
     ]
    }
   ],
   "source": [
    "ar2 = de.AudioReader(audio_file, ctx, sample_rate = 22050)\n",
    "print('Shape of audio samples: ', ar.shape())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_rate2 = 22050\n",
    "end2 = sample_rate2 * 5\n",
    "play(ar2[0:end2], 1, sample_rate2, \"demo_22050.wav\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py39-no-conda",
   "language": "python",
   "name": "py39-no-conda"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
